//
//  Root2ViewController.m
//  豆瓣综合网页
//
//  Created by dllo on 16/1/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Root2ViewController.h"
#import "UIImageView+WebCache.h"
#import "activityModel.h"
#import "ZLFDateBaseHandle.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface Root2ViewController ()
@property (nonatomic, retain) NSMutableDictionary *nameDic;
@property (nonatomic, retain) NSMutableArray *arr2Model;
//@property (nonatomic, assign) NSInteger count;
@end

@implementation Root2ViewController

- (void)dealloc {
    [_nameDic release];
    [_arr release];
    [_Dic release];
    [_indexPath release];
    Block_release(_block);
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemCompose) target:self action:@selector(itemAction:)];
    self.navigationItem.rightBarButtonItem = item;
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    [self.view addSubview:scroll];
    scroll.contentSize = CGSizeMake(0, kHeight * 3 - 200);
    [scroll release];
    UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 200, 30)];
    [scroll addSubview:titlelabel];
    [titlelabel release];
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.backgroundColor = [UIColor redColor];
//    [imgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", self.dic[@"image"]]]];
    imgView.frame = CGRectMake(10, 70, 150, 200);
    [scroll addSubview:imgView];
    [imgView release];
    UIImageView *timeimgView = [[UIImageView alloc] init];
    timeimgView.image = [UIImage imageNamed:@"time.png"];
    timeimgView.frame = CGRectMake(170, 75, 20, 20);
    [scroll addSubview:timeimgView];
    [timeimgView release];
    UIImageView *personimgView = [[UIImageView alloc] init];
    personimgView.image = [UIImage imageNamed:@"person.png"];
    personimgView.frame = CGRectMake(170, 100, 20, 20);
    [scroll addSubview:personimgView];
    [personimgView release];
    UIImageView *typeimgView = [[UIImageView alloc] init];
    typeimgView.image = [UIImage imageNamed: @"type.png"];
    typeimgView.frame = CGRectMake(170, 125, 20, 20);
    [scroll addSubview:typeimgView];
    [typeimgView release];
    UIImageView *addressView = [[UIImageView alloc] init];
    addressView.image = [UIImage imageNamed:@"address.png"];
    addressView.frame = CGRectMake(170, 150, 20, 20);
    [scroll addSubview:addressView];
    [addressView release];
    
    UILabel *timelabel = [[UILabel alloc] init];
    timelabel.frame = CGRectMake(195, 75, 180, 20);

    [scroll addSubview:timelabel];
    [timelabel release];
    
    UILabel *personlabel = [[UILabel alloc] init];

    personlabel.frame = CGRectMake(195, 100, 180, 20);
    [scroll addSubview:personlabel];
    [personlabel release];
    
    UILabel *typelabel = [[UILabel alloc] init];
    typelabel.frame = CGRectMake(195, 125, 180, 20);

    [scroll addSubview:typelabel];
    [typelabel release];
    
    UILabel *addresslabel = [[UILabel alloc] init];
    addresslabel.frame = CGRectMake(195, 150, 180, 20);

    [scroll addSubview:addresslabel];
    [addresslabel release];
    
    UIView *downView = [[UIView alloc] init];
    downView.frame = CGRectMake(10, 330, 350, 1500);

    [scroll addSubview:downView];
    [downView release];
    UILabel *downlabel = [[UILabel alloc] init];
    downlabel.frame = CGRectMake(0, 0, 350, 1500);

    [downView addSubview:downlabel];
    [downlabel release];
    UILabel *downlable1 = [[UILabel alloc] init];
    downlable1.frame = CGRectMake(0, 0, 350
                                  , 100);

    [downView addSubview:downlable1];
    [downlable1 release];
    titlelabel.text = _arr[_indexPath.row][@"title"];
    titlelabel.font = [UIFont systemFontOfSize:20];
    NSString *beginTime = _arr[_indexPath.row][@"begin_time"];
    NSString *endTime = _arr[_indexPath.row][@"end_time"];
    NSString *begin = [beginTime substringWithRange:NSMakeRange(5, 11)];
    NSString *end = [endTime substringWithRange:NSMakeRange(5, 11)];
    timelabel.text = [NSString stringWithFormat:@"%@-- %@", begin, end];
    timelabel.font = [UIFont systemFontOfSize:13];
    addresslabel.text =_arr[_indexPath.row][@"address"];
    addresslabel.font = [UIFont systemFontOfSize:13];
    typelabel.text = _arr[_indexPath.row][@"category_name"];
    typelabel.font = [UIFont systemFontOfSize:13];
    personlabel.text = _arr[_indexPath.row][@"owner"][@"name"];
    personlabel.font = [UIFont systemFontOfSize:13];
    downlabel.text = _arr[_indexPath.row][@"content"];
    downlabel.numberOfLines = 1000;
    downlable1.text = @"活动详情";
    downlable1.font = [UIFont systemFontOfSize:22];
     NSString *str = _arr[_indexPath.row][@"image"];
    [imgView sd_setImageWithURL:[NSURL URLWithString:str]];
    
    self.nameDic = [NSMutableDictionary dictionary];
    
//    self.count = 0;

}

- (void)itemAction:(UIBarButtonItem *)item {
    
//    self.count++;
//    
//    if (self.count == 1) {
//        UIAlertController *aler = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
//        [aler addAction:action2];
//        
//        [self presentViewController:aler animated:YES completion:nil];
//    }else{
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"已收藏" preferredStyle:UIAlertControllerStyleAlert];
//        
//        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            
//        }];
//        [alert addAction:action];
//        [self presentViewController:alert animated:YES completion:nil];
//    }
    
    
    
    
//    NSString *str1 = self.Dic[self.arr[_indexPath.row][@"title"]];
//    NSString *str2 = _nameDic[self.arr[_indexPath.row][@"title"]];
//    NSLog(@"啊啊啊啊%@", _nameDic[@"title"]);
//    
//    
//    if (!str1 && !str2) {
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
////        UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
////        [alert addAction:alertAction];
//        [self presentViewController:alert animated:YES completion:^{
//            
//        }];
//        
//        [self performSelector:@selector(dismissAlter) withObject:nil afterDelay:1.5];
//        
//        [_nameDic setObject:@"已收藏" forKey:_arr[_indexPath.row][@"title"]];
//        NSLog(@"%@", _nameDic);
//        NSString *str3 = _arr[_indexPath.row][@"title"];
//        self.block(str3);
//    }
//    if (str1 || str2) {
//        UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已收藏" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
//        [alert2 addAction:alertAction2];
//        [self presentViewController:alert2 animated:YES completion:^{
//            
//        }];
//    }
    NSArray *arrModel = [[ZLFDateBaseHandle shareActivity] actselectTabel];
//    NSLog(@"%@", arrModel);
    
    NSMutableDictionary *dic = self.arr[_indexPath.row];
    
    activityModel *model1 = [activityModel activitymodel];

    model1.title = dic[@"title"];
    
    NSLog(@"modal:%@", model1.title);

    NSInteger temp = 0;
    for (activityModel *model in arrModel) {
        if ([model.title isEqualToString:model1.title]) {
            temp = 1;
        }
    }
    if (!temp) {
        [[ZLFDateBaseHandle shareActivity] actinsertTabel:model1];
            
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
         
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
    }else {
        UIAlertController *alert2 = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否取消该活动" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertAction2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        UIAlertAction *alerAction3 = [UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [[ZLFDateBaseHandle shareActivity] actdeleteTabel:model1];
        }];
        [alert2 addAction:alertAction2];
        [alert2 addAction:alerAction3];
        [self presentViewController:alert2 animated:YES completion:^{
                
            }];
            
    }
    
    
    
    
    
    
    
    
    
    
    
}

- (void)dismissAlter {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
